package com.poposchool.project.controller;

import com.poposchool.project.common.Result;
import com.poposchool.project.mapper.DictMapper;
import com.poposchool.project.entity.Menu;
import com.poposchool.project.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Wang Jie
 * @Date 2022/7/5 11:03
 * @Version 1.0
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    MenuService menuService;

    @Resource
    DictMapper dictMapper;

    @GetMapping("/menuCounts")
    public Result getCounts(){
        return Result.success(menuService.getAllNum());
    }


    @GetMapping("/menus")
    public Result getMenus() {
        return Result.success(menuService.getMenusConstruction());
    }

    @GetMapping("/menu")
    public Result getMenu(Integer id) {
        return Result.success(menuService.getMenuById(id));
    }

    @PostMapping("/save")
//    如果参数什么都不加，那么就是path中传来的
    public Result saveMenu(@RequestBody Menu menu) {
        menuService.insertMenu(menu);
        return Result.success();
    }

    @PostMapping("/update")
//   @RequestParam("user")必须指定要有user这个key
    public Result updateMenu(@RequestBody Menu menu) {
        menuService.UpdateMenu(menu);
        return Result.success();
    }

    @GetMapping("/pageMenus")
    public Result getPage(
            @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize",defaultValue = "6") Integer pageSize,
            @RequestParam(name = "name",defaultValue = "") String name){
        Map<String,Object> map = new HashMap<>();
//      放入总数量
        map.put("total",menuService.getAllNum());
//      多条件查询
        map.put("menus",menuService.getPageMenus(pageNum,pageSize,name));
        return Result.success(map);
    }

    @DeleteMapping("/deleteMenu")
    public Result deleteMenu(Integer id){
        menuService.deleteMenuById(id);
        return Result.success();
    }

    @GetMapping("/icons")
    public Result getIcons(){
        return Result.success(dictMapper.finAll());
    }
}
